package cc.idoone.jasperreport.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRXlsExporter;

/**
 * BeanList 导出各种报表测试
 * 
 * @author hemj
 * @date 2017年2月14日
 */
public class JasperReportExport {

	public static void main(String[] args) {
		String sourceFileName = "samples/jrtemplates/jasper_report_template.jasper";
		DataBeanList DataBeanList = new DataBeanList();
		ArrayList<DataBean> dataList = DataBeanList.getDataBeanList();

		JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);

		try {
			Map parameters = new HashMap();
			parameters.put("ReportTitle", "List of Contacts");
			parameters.put("Author", "Prepared By Manisha");
			String jrprint = JasperFillManager.fillReportToFile(sourceFileName, parameters,
					beanColDataSource);
			System.out.println(jrprint);

			if (jrprint != null) {
				// 1- export to PDF
				JasperExportManager.exportReportToPdfFile(jrprint, "E://sample_report.pdf");

				// 2- export to HTML
				JasperExportManager.exportReportToHtmlFile(jrprint, "E://sample_report.html");

				// 3- export to Excel sheet
				JRXlsExporter exporter = new JRXlsExporter();
				exporter.setParameter(JRExporterParameter.INPUT_FILE_NAME, jrprint);
				exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "E://sample_report.xls");
				exporter.exportReport();
			}
		} catch (JRException e) {
			e.printStackTrace();
		}
	}

}
